package com.seckill.controller.admin;

import com.seckill.pojo.dto.VoucherOrderSubmitDTO;
import com.seckill.pojo.entity.SeckillCoupon;
import com.seckill.pojo.vo.VoucherOrderVo;
import com.seckill.service.SeckillCouponService;

import com.sky.result.PageResult;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  增删查改  优惠券
 *  上架优惠券到某个活动
 *  下架优惠券到某个活动
 *  秒杀优惠券    缓存穿透缓存击穿问题 解决库存超卖问题  一个限购一单  redis
 *  异步支付      rabitmq（选）
 */


/**
 * 秒杀优惠券管理接口
 */
@RestController
@RequestMapping("/admin/seckill/coupon")
@Tag(name = "秒杀优惠券管理接口")
@Slf4j
public class SeckillCouponController {

    @Autowired
    private SeckillCouponService seckillCouponService;

    /**
     * 新增秒杀优惠券
     * @param seckillCoupon
     * @return
     */
    @PostMapping
    //@ApiOperation("新增秒杀优惠券")
    public Result<String> save(@RequestBody SeckillCoupon seckillCoupon) {
        log.info("新增秒杀优惠券：{}", seckillCoupon);
        seckillCouponService.save(seckillCoupon);
        return Result.success();
    }

    /**
     * 分页查询秒杀优惠券
     * @param page
     * @param pageSize
     * @param activityId
     * @return
     */
    @GetMapping("/page")
    //@ApiOperation("分页查询秒杀优惠券")
    public Result<PageResult> page(int page, int pageSize, Long activityId) {
        log.info("分页查询秒杀优惠券：第{}页，每页{}条，活动ID：{}", page, pageSize, activityId);
        PageResult pageResult = seckillCouponService.pageQuery(page, pageSize, activityId);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询秒杀优惠券
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    //@ApiOperation("根据id查询秒杀优惠券")
    public Result<SeckillCoupon> getById(@PathVariable Long id) {
        log.info("根据id查询秒杀优惠券：{}", id);
        SeckillCoupon seckillCoupon = seckillCouponService.getById(id);
        return Result.success(seckillCoupon);
    }

    /**
     * 修改秒杀优惠券
     * @param seckillCoupon
     * @return
     */
    @PutMapping
    //@ApiOperation("修改秒杀优惠券")
    public Result<String> update(@RequestBody SeckillCoupon seckillCoupon) {
        log.info("修改秒杀优惠券：{}", seckillCoupon);
        seckillCouponService.update(seckillCoupon);
        return Result.success();
    }

    /**
     * 删除秒杀优惠券
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
   // @ApiOperation("删除秒杀优惠券")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除秒杀优惠券：{}", id);
        seckillCouponService.delete(id);
        return Result.success();
    }
    
    /**
     * 上架优惠券到活动
     * @param couponId 优惠券ID
     * @param activityId 活动ID
     * @return
     */
    @PostMapping("/addToActivity/{couponId}/{activityId}")
    //@ApiOperation("上架优惠券到活动")
    public Result<String> addToActivity(@PathVariable Long couponId, @PathVariable Long activityId) {
        log.info("上架优惠券到活动：优惠券ID={}，活动ID={}", couponId, activityId);
        seckillCouponService.addToActivity(couponId, activityId);
        return Result.success();
    }

    @PostMapping("/seckillCouponOrder/")
    @Operation(summary = "上架优惠券到活动接口")
    public Result<VoucherOrderVo> seckillCouponOrder(@RequestBody VoucherOrderSubmitDTO voucherOrderSubmitDTO) {
        log.info("正在抢购优惠券");
        VoucherOrderVo voucherOrderVo=seckillCouponService.seckillCouponOrder(voucherOrderSubmitDTO);
        if(voucherOrderVo==null){ return  Result.error("抢购失败");}
        return Result.success(voucherOrderVo);
    }
}